import myAxios from "../plugins/myAxios";
import { setCurrentUserState } from "../states/user";
import {UserType} from "../models/user";
import qs from "qs";

export const getCurrentUser = async () => {
    // const currentUser = getCurrentUserState();
    // if (currentUser) {
    //     return currentUser;
    // }
    // 不存在则从远程获取
    const res = await myAxios.get('/user/current');
    if (res.code === 0) {
        setCurrentUserState(res.data);
        return res.data;
    }
    return null;
/*
    const getCurrentUser: UserType = await myAxios.get("/user/current")
    // @ts-ignore
    sessionStorage.setItem("longUser", getCurrentUser ? JSON.stringify(getCurrentUser) : JSON.stringify({}))
    return getCurrentUser*/
}

export const getUserById = async (fromId) => {

    const res = await myAxios.get('/user/search/tags', {
        params: {
            fromId: fromId
        }
    })
 /*       paramsSerializer: params => {
            return qs.stringify(params, {indices: false})
        }*/
// 检查网络请求的结果
    if (res.code === 0) {
        // 如果请求成功并且返回的数据中 code 为 0，则返回用户数据
        return res.data;
    } else {
        // 如果请求失败或者返回的数据中 code 不为 0，则返回 null
        console.error('Failed to get user data:', res.data.message);
        return null;
    }

}

